package com.ruoyi.radarOrder.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.config.util.address.AddressUtil;
import com.ruoyi.config.util.address.WSSsdrAddress;
import com.ruoyi.order.mapper.TechnicianItemOrderMapper;
import com.ruoyi.radarOrder.domain.TechnicianRadarItemOrder;
import com.ruoyi.radarOrder.service.ITechnicianRadarItemOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 订单雷达统计Controller
 *
 * @author ruoyi
 * @date 2025-04-15
 */
@RestController
@RequestMapping("/technicianRadarItemOrder/technicianRadarItemOrder")
public class TechnicianRadarItemOrderController extends BaseController {
    @Autowired
    private ITechnicianRadarItemOrderService technicianRadarItemOrderService;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;

    /**
     * 查询订单雷达统计列表
     */
    @PreAuthorize("@ss.hasPermi('technicianRadarItemOrder:technicianRadarItemOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(TechnicianRadarItemOrder technicianRadarItemOrder) {
        if (!CheckEmptyUtil.isEmpty(technicianRadarItemOrder.getUserName())) {
            List<Long> userIds = technicianItemOrderMapper.seleUserId(technicianRadarItemOrder.getUserName());
            if (!CheckEmptyUtil.isEmpty(userIds)) {
                technicianRadarItemOrder.setUserIds(userIds);
            } else {
                return getDataTable(Collections.emptyList());
            }
        } else {
            technicianRadarItemOrder.setUserIds(Collections.emptyList());
        }
        startPage();
        List<TechnicianRadarItemOrder> list = technicianRadarItemOrderService.selectTechnicianRadarItemOrderList(technicianRadarItemOrder);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianRadarItemOrder itemOrder : list) {
                String tName = technicianItemOrderMapper.selectTName(itemOrder.getTid());
                itemOrder.setTName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                String userName = technicianItemOrderMapper.selectUserName(itemOrder.getUserId());
                itemOrder.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
                WSSsdrAddress wsSsdrAddress = AddressUtil.addressFormat(itemOrder.getAddressDetail());
                String province = !CheckEmptyUtil.isEmpty(wsSsdrAddress.getProvince()) ? wsSsdrAddress.getProvince() : "";
                String city = !CheckEmptyUtil.isEmpty(wsSsdrAddress.getCity()) ? wsSsdrAddress.getCity() : "";
                String county = !CheckEmptyUtil.isEmpty(wsSsdrAddress.getCounty()) ? wsSsdrAddress.getCounty() : "";
                itemOrder.setServiceAddress(province+" "+city+" "+county);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出订单雷达统计列表
     */
    @PreAuthorize("@ss.hasPermi('technicianRadarItemOrder:technicianRadarItemOrder:export')")
    @Log(title = "订单雷达统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TechnicianRadarItemOrder technicianRadarItemOrder) {
        if (!CheckEmptyUtil.isEmpty(technicianRadarItemOrder.getUserName())) {
            List<Long> userIds = technicianItemOrderMapper.seleUserId(technicianRadarItemOrder.getUserName());
            if (!CheckEmptyUtil.isEmpty(userIds)) {
                technicianRadarItemOrder.setUserIds(userIds);
            }
        }
        List<TechnicianRadarItemOrder> list = technicianRadarItemOrderService.selectTechnicianRadarItemOrderList(technicianRadarItemOrder);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianRadarItemOrder itemOrder : list) {
                String tName = technicianItemOrderMapper.selectTName(itemOrder.getTid());
                itemOrder.setTName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                String userName = technicianItemOrderMapper.selectUserName(itemOrder.getUserId());
                itemOrder.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);

                WSSsdrAddress wsSsdrAddress = AddressUtil.addressFormat(itemOrder.getAddressDetail());
                String province = !CheckEmptyUtil.isEmpty(wsSsdrAddress.getProvince()) ? wsSsdrAddress.getProvince() : "";
                String city = !CheckEmptyUtil.isEmpty(wsSsdrAddress.getCity()) ? wsSsdrAddress.getCity() : "";
                String county = !CheckEmptyUtil.isEmpty(wsSsdrAddress.getCounty()) ? wsSsdrAddress.getCounty() : "";
                itemOrder.setServiceAddress(province+" "+city+" "+county);
            }
        }
        ExcelUtil<TechnicianRadarItemOrder> util = new ExcelUtil<TechnicianRadarItemOrder>(TechnicianRadarItemOrder.class);
        util.exportExcel(response, list, "订单雷达统计数据");
    }

    /**
     * 获取订单雷达统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('technicianRadarItemOrder:technicianRadarItemOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(technicianRadarItemOrderService.selectTechnicianRadarItemOrderById(id));
    }

    /**
     * 新增订单雷达统计
     */
    @PreAuthorize("@ss.hasPermi('technicianRadarItemOrder:technicianRadarItemOrder:add')")
    @Log(title = "订单雷达统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TechnicianRadarItemOrder technicianRadarItemOrder) {
        return toAjax(technicianRadarItemOrderService.insertTechnicianRadarItemOrder(technicianRadarItemOrder));
    }

    /**
     * 修改订单雷达统计
     */
    @PreAuthorize("@ss.hasPermi('technicianRadarItemOrder:technicianRadarItemOrder:edit')")
    @Log(title = "订单雷达统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TechnicianRadarItemOrder technicianRadarItemOrder) {
        return toAjax(technicianRadarItemOrderService.updateTechnicianRadarItemOrder(technicianRadarItemOrder));
    }

    /**
     * 删除订单雷达统计
     */
    @PreAuthorize("@ss.hasPermi('technicianRadarItemOrder:technicianRadarItemOrder:remove')")
    @Log(title = "订单雷达统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(technicianRadarItemOrderService.deleteTechnicianRadarItemOrderByIds(ids));
    }
}
